package DB;

import Entity.CustomerCard;

/** this class asks queries to get and insert customer's history from and into the db  */
public class CustomerHistoryQuery {
	public String [] ProductList=new String[256];
	
	/**function which insert customer's history into db */
	public static String InsertCustomerCardQuery(CustomerCard Cst) 
	{
		return  "INSERT INTO cst_history (SaleID , CustomerID, ContactCustomer,ProductBought, CustomerResponse, CustomerApproval, CallTimeMin) VALUES ("
				+ Cst.getCSaleID()
				+ ",\""
				+ Cst.getCCustomerID()
				+ "\",\""
				+ Cst.getCProductName()
				+ "\",\""
				+ Cst.getCContactCust()
				+ "\",\""
				+ Cst.getCustResponse()
				+ "\",\""
				+ Cst.getCallTime()
				+ "\",\""
				+ Cst.getCustApproval()
				+ "\")";
	}

	/**function which get customer's history from db*/
	public static String getCustomerCardQuery(String customerID, String saleid) 
	{
		String sql ="SELECT * FROM cst_history WHERE CustomerID = " + "\"" + customerID + "\" AND SaleID = " + "\"" + saleid + "\"";
		return sql;
	}

	public static String getAllCustomers(String workerid, String saleid)
	{
		String sql = "SELECT CustomerID FROM cst_history WHERE WorkerID = " + "\"" + workerid + "\" AND SaleID = " + "\"" + saleid + "\"";
		return sql;
	}

	/**function which check if city is in db */
	public static String checkExistCity(String CustomerID)
	{
		String str="SELECT (CustomerID) FROM cst_history WHERE CustomerID = " + "\"" + CustomerID + "\"";
		return str;
	}
	
	/**function which get product name from products card db*/
	public static String getProductsCard() 
	{
		return "SELECT ProductName FROM products" ;
	}
	
	public static String findCustomerbought(CustomerCard customerCard,String saleid) 
	{
		String sql = "SELECT COUNT(ProductBought) FROM cst_history WHERE CustomerID = " + "\"" + customerCard.getCCustomerID() + "\" AND SaleID = " + "\"" + saleid + "\"";
		return sql;
	}

	/**functions which update customer card's details*/
	public static String updateCustomerInfo(CustomerCard customerCard,String saleid, int count) 
	{		
		String sql = "UPDATE `customers` SET `PurchaseCounter`='" +count
				+ "'WHERE CustomerID = '" + customerCard.getCCustomerID() + "'";
		return sql;
	}

	public static String updateCustomerCard(CustomerCard customerCard, String saleid) 
	{
		String sql;
		if (customerCard.getCProductName()=="")
			 sql = "UPDATE `cst_history` SET `ContactCustomer`='"
					+ customerCard.getCContactCust() +  "', `CustomerResponse`='" 
					+ customerCard.getCustResponse() +  "', `CallTimeMin`='" 
					+ customerCard.getCallTime() +  "', `CustomerApproval`='" 
					+ customerCard.getCustApproval()
					+ "' WHERE `CustomerID`='" + customerCard.getCCustomerID() + "' AND `SaleID` = '" + saleid + "'";

		else if (customerCard.getCustResponse()=="")
			 sql = "UPDATE `cst_history` SET `ContactCustomer`='"
					+ customerCard.getCContactCust() + "', `ProductBought`='"
					+ customerCard.getCProductName() +  "', `CallTimeMin`='" 
					+ customerCard.getCallTime() +  "', `CustomerApproval`='" 
					+ customerCard.getCustApproval()
					+ "' WHERE `CustomerID`='" + customerCard.getCCustomerID() + "' AND `SaleID` = '" + saleid + "'";
		
		else if ((customerCard.getCProductName()=="") && (customerCard.getCustResponse()==""))
			 sql = "UPDATE `cst_history` SET `ContactCustomer`='"
					+ customerCard.getCContactCust() +  "', `CallTimeMin`='" 
					+ customerCard.getCallTime() +  "', `CustomerApproval`='" 
					+ customerCard.getCustApproval()
					+ "' WHERE `CustomerID`='" + customerCard.getCCustomerID() + "' AND `SaleID` = '" + saleid + "'";
		else 
			 sql = "UPDATE `cst_history` SET `ContactCustomer`='"
					+ customerCard.getCContactCust() + "', `ProductBought`='"
					+ customerCard.getCProductName() + "', `CustomerResponse`='" 
					+ customerCard.getCustResponse() +  "', `CallTimeMin`='" 
					+ customerCard.getCallTime() +  "', `CustomerApproval`='" 
					+ customerCard.getCustApproval()
					+ "' WHERE `CustomerID`='" + customerCard.getCCustomerID() + "' AND `SaleID` = '" + saleid + "'";
		return sql;
	}
	
	/**functions which check if a field is empty */
	public boolean isEmpty(String field) 
	{
		if (field.isEmpty())
			return true;
		return false;
	}

	public boolean isEmpty(int field) 
	{
		if (field != 0)
			return true;
		return false;
	}
}

